﻿using System;
using LiteOffice.Models.Grids;
using Trirand.Web.Mvc;
using System.Text;

namespace LiteOffice.Extensions
{
	public static class LiteGrid
	{
		public static TGrid AsStandardGrid<TGrid>(this LiteGrid<TGrid> source, string ID, string dataUrl)
			where TGrid : LiteGrid<TGrid>, new()
		{
			return AsStandardGrid(source, ID, dataUrl, null);
		}

		public static TGrid AsStandardGrid<TGrid>(this LiteGrid<TGrid> source, string ID, string dataUrl, string editUrl)
			where TGrid : LiteGrid<TGrid>, new()
		{
			source.ID = ID;
			source.DataUrl = dataUrl;

			if (!String.IsNullOrEmpty(editUrl))
			{
				source.EditUrl = editUrl;

				source.ToolBarSettings.ShowAddButton = true;
				source.ToolBarSettings.ShowDeleteButton = true;
				source.ToolBarSettings.ShowEditButton = true;
			}

			Action<JQGridColumn> makeColumnsUnsearchable = (c) =>
				{
					c.Searchable = false;
				};

			source.Columns.ForEach(makeColumnsUnsearchable);
			
			return (TGrid)source;
		}

		public static TGrid AsEditGrid<TGrid>(this LiteGrid<TGrid> source, string ID, string dataUrl, string editUrl)
			where TGrid : LiteGrid<TGrid>, new()
		{
			return asEditGrid(source, ID, dataUrl, editUrl, HierarchyMode.None, "", null);
		}

		public static TGrid AsParentEditGrid<TGrid>(this LiteGrid<TGrid> source, string ID, string dataUrl, string editUrl, string childGridID)
			where TGrid : LiteGrid<TGrid>, new()
		{
			return asEditGrid(source, ID, dataUrl, editUrl, HierarchyMode.Parent, null, childGridID);
		}

		public static TGrid AsChildEditGrid<TGrid>(this LiteGrid<TGrid> source, string ID, string dataUrl, string editUrl, string parentGridID)
			where TGrid : LiteGrid<TGrid>, new()
		{
			return asEditGrid(source, ID, dataUrl, editUrl, HierarchyMode.Child, parentGridID, null);
		}

		public static TGrid AsParentAndChildEditGrid<TGrid>(this LiteGrid<TGrid> source, string ID, string dataUrl, string editUrl, string parentGridID, string childGridID)
			where TGrid : LiteGrid<TGrid>, new()
		{
			return asEditGrid(source, ID, dataUrl, editUrl, HierarchyMode.ParentAndChild, parentGridID, childGridID);
		}

		private static TGrid asEditGrid<TGrid>(this LiteGrid<TGrid> source, string ID, string dataUrl, string editUrl, HierarchyMode hierarchyMode, string parentGridID, string childGridID)
			where TGrid : LiteGrid<TGrid>, new()
		{
			TGrid editGrid = AsStandardGrid(source, ID, dataUrl, editUrl);

			editGrid.Columns.Add(new JQGridColumn
			{
				DataField = "Edit",
				HeaderText = "&nbsp",
				Width = 100, 
				Searchable = false
			});

			editGrid.HierarchySettings.HierarchyMode = hierarchyMode;

			if (hierarchyMode == HierarchyMode.Parent || hierarchyMode == HierarchyMode.None)
			{
				StringBuilder sb = new StringBuilder();
				sb.AppendLine("initEditGrid('" + editGrid.ID + "');");

				editGrid.ClientSideEvents.GridInitialized = sb.ToString();
			}
			else
			{
				editGrid.ParentGridID = parentGridID;

				editGrid.ClientSideEvents.GridInitialized = "initChildEditGrids('" + parentGridID + "');";
			}

			if (hierarchyMode == HierarchyMode.Parent || hierarchyMode == HierarchyMode.ParentAndChild)
			{
				StringBuilder sb = new StringBuilder();
				sb.AppendLine("function(subgrid_id, row_id, message, suffix) {");
				sb.AppendLine(" showSubGrid_" + childGridID + "(subgrid_id, row_id);");
				sb.AppendLine("	showSubGridAddNewButton(subgrid_id);");
				sb.AppendLine("}");

				editGrid.ClientSideEvents.SubGridRowExpanded = sb.ToString();
			}

			source.ToolBarSettings.ShowAddButton = false;
			source.ToolBarSettings.ShowDeleteButton = false;
			source.ToolBarSettings.ShowEditButton = false;

			return editGrid;
		}
	}
}